import instance from "./http.interceptor";

// 登录状态拦截
const mainCode = [555, 10001];
const proCode = ["token-not-valid", "login-required", 1003, 301001];
const ucCode = [512, 403];
const otcCode = [401];
const poolCode = [100001];
const dmCode = [1011];
const nftCode = [115, 116, 125];
export const authContact = [
  ...mainCode,
  ...proCode,
  ...ucCode,
  ...otcCode,
  ...poolCode,
  ...dmCode,
  ...nftCode,
];

const tokenKeys = (keys) => {
  const keysList = {
    phoneApi: {
      "token":
      sessionStorage.getItem("PHONE-TOKEN")
    },
  };
  return keysList[keys];
};

const responseFormat = (fn) => {
  return new Promise((resolve) => {
    fn.then((v) => resolve(v.data)).catch((k) => resolve(k));
  });
};

class Https {
  constructor(apiForm) {
    this.apiKeys = apiForm;
    this.tokenKey = tokenKeys(apiForm);
  }
  get(url, { data = {}, params = {}, headers = {'Content-Type':'multipart/form-data'} }) {
    this.tokenKey = tokenKeys(this.apiKeys);
    Object.assign(headers, this.tokenKey);
    return responseFormat(
      instance({ url, method: "get", data, params, headers })
    );
  }
  post(url, { data = {}, params = {}, headers = {'Content-Type':'multipart/form-data'} }) {
    this.tokenKey = tokenKeys(this.apiKeys);
    Object.assign(headers, this.tokenKey);
    return responseFormat(
      instance({ url, method: "post", data, params, headers })
    );
  }
  delete(url, { data = {}, params = {}, headers = {'Content-Type':'multipart/form-data'} }) {
    this.tokenKey = tokenKeys(this.apiKeys);
    Object.assign(headers, this.tokenKey);
    return responseFormat(
      instance({ url, method: "delete", data, params, headers })
    );
  }
  put(url, { data = {}, params = {}, headers = {'Content-Type':'multipart/form-data'} }) {
    this.tokenKey = tokenKeys(this.apiKeys);
    Object.assign(headers, this.tokenKey);
    return responseFormat(
      instance({ url, method: "put", data, params, headers })
    );
  }
}

export const phoneApi = new Https("phoneApi");

